package ift604.projet;

import static ift604.projet.music.Playlist.emptyPlaylist;
import static ift604.projet.music.SQLPlaylistRepository.SQLPlaylistRepositoryWithContext;
import ift604.projet.feeling.Feeling;
import ift604.projet.feeling.UserFeelingUpdatedListener;
import ift604.projet.music.Playlist;
import ift604.projet.music.PlaylistUpdatedListener;
import ift604.projet.music.SQLPlaylistRepository;

import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.util.Log;

public class MapMyFeelingsApplication extends Application implements PlaylistUpdatedListener, OnSharedPreferenceChangeListener  {
	private static final String TAG = MapMyFeelingsApplication.class.getSimpleName();

    List<UserFeelingUpdatedListener> updatedCurrentUserFeelingListeners = new ArrayList<UserFeelingUpdatedListener>();
    Playlist currentPlaylist = emptyPlaylist();
    public SQLPlaylistRepository sqlPlaylistRepository = SQLPlaylistRepositoryWithContext(this);
    
    public MapMyFeelingsActivity mapMyFeelingsActivity;
    public UpdaterService updaterService;

    Feeling currentUserFeeling;
    
    private SharedPreferences prefs;  

	public void onCreate() {
		super.onCreate();
		
	    this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
	    this.prefs.registerOnSharedPreferenceChangeListener(this);

		Log.i(TAG, "onCreated");
	}

	public void onTerminate() {
		super.onTerminate();
		Log.i(TAG, "onTerminated");
	}

    public void addUpdateFeelingListener(UserFeelingUpdatedListener listener) {
        updatedCurrentUserFeelingListeners.add(listener);
    }

    public Feeling currentUserFeeling(){
        return currentUserFeeling;
    }

    public MapMyFeelingsApplication currentUserFeeling(Feeling currentUserFeeling){
        this.currentUserFeeling = currentUserFeeling;
        for (UserFeelingUpdatedListener listener : updatedCurrentUserFeelingListeners) {
            listener.feelingOfUserUpdated(currentUserFeeling);
        }
        return this;
    }

    public Playlist currentPlaylist(){
        return currentPlaylist;
    }

    public MapMyFeelingsApplication currentPlaylist(Playlist playlist){
        this.currentPlaylist = playlist;
        return this;
    }

    @Override
    public void playlistUpdated(Playlist playlist) {
        currentPlaylist = playlist;
        sqlPlaylistRepository.updatePlaylistOfFeeling(playlist, currentUserFeeling);
    }

    
	@Override
	public void playlistUpdateFailed() {

	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sp, String cle) {
		if (cle.equalsIgnoreCase("delay")) {
			String valStr = sp.getString(cle, "");
			int val;
			try {
				val = Integer.parseInt(valStr);
			}
			catch (Exception e) {
				val = 1;
			}

			if (val < 1) 
				val = 1;
			
			UpdaterService.DELAY = val*60000;
		}
		
		if (cle.equalsIgnoreCase("centrergps")) {
			boolean val = sp.getBoolean(cle, false);
			mapMyFeelingsActivity.centrerSurGPS = val;
		}
	}
}